export default {
  namespaced: true,
  state: {
    username: localStorage.getItem('username'),
    phone: localStorage.getItem('phone'),
    token: localStorage.getItem('token'),
    routes: JSON.parse(localStorage.getItem('routes') || '[]'),//后端返回的
    initRoutes: [],//处理之后的路由
    navTree: JSON.parse(localStorage.getItem('navTree'))
  },
  getters: {
 
  },
  mutations: {
    loginMutatin(state, user) {
      state.username = user.username;
      state.phone = user.phone;
      state.token = user.token;
    },
    logoutMutatin(state) {
      state.username = null;
      state.phone = null;
      state.token = null;
    },
    
    //储存登录用户的权限
    authorityMutatin(state, routes) {
      state.routes = routes;
    },
    setToken(state, token) {
      state.token = token
    },
    setRoutes(state, routes) {
      state.routes = routes;
    },
    setInitRoutes(state, getRouter) {
      state.initRoutes = getRouter
    }
  },
  actions: {
    loginAction({ commit }, user) {
      return new Promise((resolve) => {
        commit('loginMutatin', {
          username: user.username,
          phone: user.phone,
          token: user.token,
        });
        localStorage.setItem('username', user.username);
        localStorage.setItem('phone', user.phone);
        localStorage.setItem('token', user.token);
        resolve();
      })
    },
    logoutAction({ commit }) {
      return new Promise((resolve) => {
        commit('logoutMutatin');
        localStorage.clear(),
          resolve();
      })
    },
    //权限
    authorityAction({ commit }, user) {
      var authoritys = user.authority;
      // console.log(user.authority);
      for (let i = 0; i < authoritys.length; i++) {
        let convert = authoritys[i].name

        if (authoritys[i].menuID == 2) {
          // console.log('zhuye ');
          authoritys[i].pid = 0;
        }
        if (convert) {
          // console.log('当前权限有值');
          let arr = convert.split(':')
          if (arr.length == 1) {
            arr.push('View');
          }
          let newArr = [];
          for (let j = 0; j < arr.length; j++) {
            // console.log('字母转大写');
            let data1 = arr[j].slice(0, 1);
            let data2 = arr[j].slice(1);
            let data = data1.toUpperCase() + data2;
            newArr.push(data)
          }
          convert = newArr.join('')
          authoritys[i].component = convert;
          if (authoritys[i].menuID == 1) {
            //主页框架特殊处理
            authoritys[i].path = '/';

          } else if (authoritys[i].menuID == 2){
            authoritys[i].path = 'IndexShow';
            // authoritys[i].redirect = ""
          } else {
            // let sss = authoritys[i].menuUrl
            // let ccc = sss.substr(1)
            authoritys[i].path = authoritys[i].menuUrl;
            // console.log(authoritys[i].path);
          }

          if (authoritys[i].pid == 0) {
            authoritys[i].hidden = 1;
            authoritys[i].icon = authoritys[i].component
            authoritys[i].meta = {
              index: 0,
              showFooter: true
            }
          } else {
            authoritys[i].hidden = 0;
            authoritys[i].meta = {

              index: 0,
              showFooter: true
            }
          }
        }
      }
      let aaa = authoritys[0];
      let bbb = authoritys.slice(1)
      // console.log(aaa, bbb);
      // console.log(user.authority[1]);
      aaa.children = [
        ...bbb
      ]
      authoritys = JSON.stringify(aaa)
      authoritys = JSON.parse(authoritys)
      // console.log('aaa', authoritys);


      //权限结构处理
      // console.log(authoritys);
      // console.log(authoritys.children);
      // let ccc = authoritys.children;
      let firstDeal = arrayToTree(aaa.children);
      let navTree=[];
      // console.log(firstDeal);
      function arrayToTree(items) {
        const result = []; // 存放结果集
        const itemMap = {}; //
        for (const item of items) {
          const menuID = item.menuID;
          const pid = item.pid;
          if (!itemMap[menuID]) {
            itemMap[menuID] = {
              children: [],
            }
          }
          itemMap[menuID] = {
            ...item,
            children: itemMap[menuID]['children']
          }
          const treeItem = itemMap[menuID];
          if (pid === 0) {
            result.push(treeItem);
          } else {
            if (!itemMap[pid]) {
              itemMap[pid] = {
                children: [],
              }
            }
            itemMap[pid].children.push(treeItem)
          }
        }
        return result;
      }
      // console.log(firstDeal);
      navTree = firstDeal;
      //导航结构数的重构
      let newTree=[];
      navTree.map(item=>{
        if(item.menuID==2){
          newTree.unshift(item)
        }else{
          newTree.push(item)
        }
      })
      // console.log(newTree);
      // console.log(navTree);
      navTree = newTree
      localStorage.setItem('navTree', JSON.stringify(navTree))
      let routes = [
        authoritys
      ];
      // console.log(routes);

      routes.push({
        path: '*', redirect: '/404', title: '404', hidden: 0//404 放最后
      });
      // console.log(routes);
      commit('authorityMutatin', routes);
      // console.log(routes);
      localStorage.setItem('routes', JSON.stringify(routes))
    },
    setRoutesActions({ commit }, routes) {
      commit('setRoutes', routes);
      localStorage.setItem('routes', JSON.stringify(routes));
    },
    setTokenActions({ commit }, token) {
      commit('setToken', token);
    },
    setInitRoutesActions({ commit }, initRoutes) {
      // console.log('aaaaaaaaaaa', initRoutes);
      commit('setInitRoutes', initRoutes);
    }
  }
}